package mt.controller;

import lombok.RequiredArgsConstructor;
import mt.entity.Order;
import mt.service.OrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequiredArgsConstructor
public class OrderController {
    private final OrderService service;

    @GetMapping("/orderList")
    public String toOrderList(Model model) {
        List<Order> list = service.list();
        model.addAttribute("list", list);
        return "orderList";
    }

    @GetMapping("/orderCreate")
    public String toUserCreate() {
        return "/orderCreate";
    }

    @GetMapping("/orderUpdate")
    public String toOrderUpdate(Integer id, Model model) {
        Order order = service.getById(id);
        model.addAttribute("order", order);
        return "orderUpdate";
    }




    @PostMapping("/orderList")
    public String orderList(String name, Model model) {
        List<Order> list = service.listByName(name);
        model.addAttribute("name",name);
        model.addAttribute("list", list);
        return "orderList";
    }

    @PostMapping("/orderCreate")
    public String orderCreate(String name,String phone, BigDecimal cost) {
        Order order = new Order();
        order.setName(name);
        order.setPhone(phone);
        order.setCost(cost);
        service.save(order);
        return "redirect:orderList";
    }

    @PostMapping("/orderUpdate")
    public String orderUpdate( Order order) {
        service.updateById(order);
        return "redirect:orderList";
    }

    @GetMapping("/orderRemove")
    public String orderRemove( Integer id) {
        service.removeById(id);
        return "redirect:orderList";
    }

}
